package com.example.cnotebook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.cnotebook.util.OKHttpUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends BaseActivity implements View.OnClickListener, OKHttpUtil.CallBack {
    private ImageView ivBack;
    private EditText etPassword;
    private EditText etConfirmPassword;
    private EditText etNickname;
    private EditText etEmail;
    private EditText etPhone;
    private RadioGroup rgSex;
    private RadioButton rbBoy;
    private RadioButton rbGirl;
    private Button btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


        ivBack = (ImageView) findViewById(R.id.iv_back);

        ivBack = findViewById(R.id.iv_back);
        etPassword = findViewById(R.id.et_password);
        etConfirmPassword = findViewById(R.id.et_confirm_password);
        etNickname = findViewById(R.id.et_nickname);
        etEmail = findViewById(R.id.et_email);
        etPhone = findViewById(R.id.et_phone);
        rgSex = findViewById(R.id.rg_sex);
        rbBoy = findViewById(R.id.rb_boy);
        rbGirl = findViewById(R.id.rb_girl);
        btnRegister = findViewById(R.id.btn_register);

        etPassword = (EditText) findViewById(R.id.et_password);
        btnRegister = (Button) findViewById(R.id.btn_register);

        ivBack.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_register:
                register();
                break;
        }
    }

    private void register() {
        String nickName = etNickname.getText().toString();
        String email = etEmail.getText().toString();
        String password = etPassword.getText().toString();
        String confirmPassword = etConfirmPassword.getText().toString();
        String phone = etPhone.getText().toString();
        boolean checked = rbBoy.isChecked();
        Integer sex = checked ? 1 : 0;
        if (nickName.equals("") | password.equals("") | confirmPassword.equals("")) {
            Toast.makeText(this, "请填写昵称或密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!confirmPassword.equals(password)) {
            Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("nickname", nickName);
            jsonObject.put("password", password);
            jsonObject.put("sex", sex);
            jsonObject.put("email", email);
            jsonObject.put("phone", phone);
            OKHttpUtil.post("/user/register", jsonObject, this, this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSuccess(JSONObject response) throws JSONException {
        if (response.getInt("code") == 1000) {
            String account = response.getString("data");
            Toast.makeText(this, "注册成功!", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this, LoginActivity.class);
            intent.putExtra("account", account);
            setResult(1, intent);
            finish();
        } else {
            Toast.makeText(this, "注册失败!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(int errorNo, String strMsg) {

    }
}
